package com.cps116.friendstracker;

//Point lastPoint = null;
//
////TODO: check if the items are given to you in the order you wanted...please be linear!!!
//for(OverlayItem item : overlays){
//
//	Point point = new Point();
//	projection.toPixels(item.getPoint(), point);
//
//	// if it is not the first point...we want to connect the two point and draw the line
//	//we can't connect the first point b/c it's null
//	if(lastPoint != null)	{       
//		Log.d("MapPathOverlay", "drawing a line now with points: " + "p1:" + point.x + " " + point.y + " p2: " +lastPoint.x + " " + lastPoint.y);
//		canvas.drawLine((float) point.x, (float) point.y, (float) lastPoint.x,
//				(float) lastPoint.y, paint);
//	}
//	
//	//update the last point
//	lastPoint = point;
//}

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class MapPathOverlay extends ItemizedOverlay<OverlayItem> {

	private List<OverlayItem> overlays;
	private FriendsFinderMapActivity mapActivity;

	public MapPathOverlay(Drawable icon, FriendsFinderMapActivity context) {
		super(boundCenterBottom(icon));
		this.mapActivity = context;
		overlays = new ArrayList<OverlayItem>();
	}


	public MapPathOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}

	@Override
	protected OverlayItem createItem(int i) {
		return overlays.get(i);
	}

	@Override
	public int size() {
		return overlays.size();
	}


	@Override
	protected boolean onTap(int index) {
		OverlayItem item = overlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(mapActivity);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();
		return true;
	}


	/**
	 * Here's where we are drawing takes place...we just want to draw a bunch of points and then connect the 
	 * dots... In add/delete, we want to modify redraw.
	 * Be sure to clean the canvas if needed.
	 */
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {

		//prevent redundant drawing for our case...
		if(shadow == false && overlays.size() >= 2)
		{
			Log.d("MapPathOverlay", "started drawing");

			//this is where you define how you draw the path
			//iterate through your points and draw on here
			//I think you might have to clear your canvas
			Paint paint = new Paint();
			paint.setColor(Color.RED);
			paint.setStrokeWidth(1);
		
			//create a path...
			Path path = new Path();
			Projection projection = mapView.getProjection();	
			Point startpt = new Point();
			
			projection.toPixels(overlays.get(0).getPoint(), startpt);
			path.moveTo(startpt.x, startpt.y);
			
			for(int i = 1; i < overlays.size(); i++)
			{
				OverlayItem item = overlays.get(i);
				Point pt = new Point();
				projection.toPixels(item.getPoint(), pt);
				path.lineTo(pt.x, pt.y);
			}
			
			canvas.drawPath(path, paint);
	        int size = overlays.size();
	        Point lastPoint = new Point();
	        projection.toPixels(overlays.get(0).getPoint(), lastPoint);
	        Point point = new Point();
	        for(int i = 1; i<size; i++){
	            projection.toPixels(overlays.get(i).getPoint(), point);
	            canvas.drawLine((float)lastPoint.x, (float)lastPoint.y, (float)point.x, (float)point.y, paint);
	            lastPoint = point;
	        }
	        
	        
	        
		}
		super.draw(canvas, mapView, shadow);
	}



	protected void removeOverlay(OverlayItem o){
		overlays.remove(o);
		setLastFocusedIndex(-1);
		populate();
	}

	public void addOverlay(OverlayItem o){
		//added overlay item
		Log.d("MapPathOverlay", "added overlay item");
		overlays.add(o);
		setLastFocusedIndex(-1);
		populate();
	}

	public GeoPoint getLastGeopoint() {
		
		if (overlays == null)
			overlays = new ArrayList<OverlayItem>();
		
		if(overlays.isEmpty())
			return new GeoPoint(0,0);

		//get the last geopoint...so that we can set the center to that point...
		return overlays.get(overlays.size() - 1).getPoint();
	}
	
	
}
